package se.energimyndigheten.lampguiden.lighting;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

import org.json.JSONException;

import java.io.IOException;

import se.energimyndigheten.lampguiden.HasInfoPage;
import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.SwappableListFragment;
import se.energimyndigheten.lampguiden.util.AssetsUtil;

/**
 * This is the list of different types rooms - reached from navigation drawer "Inred med ljus"
 */
public class LightingListFragment extends SwappableListFragment implements OnItemClickListener, HasInfoPage {

    private static final String TAG = LightingListFragment.class.getSimpleName();
    private static final String INFO_PATH = "Help/sv_Lighting.html";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(true);
        setHasOptionsMenu(true);
        return inflater.inflate(R.layout.lighting_activity, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        try {
            final FragmentActivity parent = this.getActivity();
            // get rooms from json
            Room[] rooms = AssetsUtil.getRoomsFromJson(parent.getAssets());
            String[] names = new String[rooms.length];
            for (int i = 0, N = rooms.length; i < N; i++) {
                names[i] = rooms[i].getName();
            }
            setListAdapter(new ArrayAdapter<>(parent, R.layout.lighting_listitem, R.id.listitem, names));

        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.actionbar_info, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(getActivity(), RoomGalleryActivity.class);
        i.putExtra(RoomGalleryActivity.EXTRA_ROOM_ID, position);
        startActivity(i);
    }

    @Override
    public String getInfoPageUrl() {
        return INFO_PATH;
    }

    @Override
    public String getInfoPageTitle() {
        return getResources().getString(R.string.section_lighting_title);
    }
}
